﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Runtime.Serialization;
using KPIS.GERP.Models.HRM;

namespace KPIS.GERP.Models.SYS
{
    [DataContract, Table("SysTaskHistory")]
    public class TaskHistory
    {
        [DataMember, Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public virtual int TaskHistoryId { get; set; }

        [DataMember, ForeignKey("ApprovePathSimulate")]
        public virtual int? SimId { get; set; }

        [DataMember]
        public virtual ApprovePathSimulate ApprovePathSimulate { get; set; }

        #region Condition

        // Condition
        [DataMember]
        public virtual ApproverType ApproverType { get; set; }

        [DataMember]
        public virtual string ApproverTypeText { get; set; }

        [DataMember, ForeignKey("ApproverPosition")]
        public int? ApproverPosId { get; set; }

        [DataMember]
        public Position ApproverPosition { get; set; }

        [DataMember]
        public virtual bool IsApproveAble { get; set; }

        [DataMember]
        public virtual bool IsForwardable { get; set; }

        [DataMember]
        public virtual bool IsRejectable { get; set; }

        [DataMember]
        public virtual bool IsPendable { get; set; }

        [DataMember]
        public virtual bool IsSendBackable { get; set; }
        
        [DataMember]
        public bool IsInRequiringSendBack { get; set; }

        [DataMember]
        public int? StepNo { get; set; }
         
        [DataMember]
        public virtual int? ApprovedBy { get; set; }

        [DataMember]
        public virtual string ApprovedByPosName { get; set; }

        [DataMember]
        public virtual string ApprovedByFullName { get; set; }

        [DataMember]
        public virtual DateTime? ApprovedWhen { get; set; }

        [DataMember]
        public virtual int? RejectedBy { get; set; }

        [DataMember]
        public virtual string RejectedByPosName { get; set; }

        [DataMember]
        public virtual string RejectedByFullName { get; set; }

        [DataMember]
        public virtual DateTime? RejectedWhen { get; set; }

        [DataMember]
        public virtual int? CanceledBy { get; set; }

        [DataMember]
        public virtual string CanceledByPosName { get; set; }

        [DataMember]
        public virtual string CanceledByFullName { get; set; }

        [DataMember]
        public virtual DateTime? CanceledWhen { get; set; }

        [DataMember]
        public virtual int? SendBackBy { get; set; }

        [DataMember]
        public virtual string SendBackByPosName { get; set; }

        [DataMember]
        public virtual string SendBackByFullName { get; set; }

        [DataMember]
        public virtual DateTime? SendBackWhen { get; set; }

        [DataMember]
        public virtual int? SendBackOkBy { get; set; }

        [DataMember]
        public virtual string SendBackOkByPosName { get; set; }

        [DataMember]
        public virtual string SendBackOkByFullName { get; set; }

        [DataMember]
        public virtual DateTime? SendBackOkWhen { get; set; }

        [DataMember]
        public virtual int? ForwardBy { get; set; }

        [DataMember]
        public virtual string ForwardByPosName { get; set; }

        [DataMember]
        public virtual string ForwardByFullName { get; set; }

        [DataMember]
        public virtual DateTime? ForwardWhen { get; set; }

        #endregion

        #region Created By

        [DataMember, DefaultValue("U")]
        public virtual string RecordStatus { get; set; }

        [DataMember]
        public virtual int? CreatedBy { get; set; }

        [DataMember]
        public virtual DateTime? CreatedWhen { get; set; }

        [DataMember]
        public virtual int? UpdatedBy { get; set; }

        [DataMember]
        public virtual DateTime? UpdatedWhen { get; set; }

        #endregion

        [DataMember, ForeignKey("Task")]
        public int? TaskId { get; set; }

        [DataMember, ForeignKey("FromTaskHistory")]
        public int? FromTaskHistoryId { get; set; } 

        [DataMember]
        public virtual Task Task { get; set; }

        [DataMember]
        public virtual TaskHistory FromTaskHistory { get; set; }

        [DataMember]
        public virtual bool PassStep { get; set; }

        [DataMember]
        public virtual string ResultStep { get; set; }

        [DataMember]
        public virtual string Remark { get; set; }

        [DataMember]
        public string SystemRemark { get; set; }

        [DataMember]
        public string ActionCode { get; set; }

        [DataMember]
        public int? ActionBy { get; set; }

        [DataMember]
        public string ActionByPosName { get; set; }

        [DataMember]
        public string ActionByFullName { get; set; }

        public bool UpdateFieldByCommandAction(CommandAction action, int byEmpId, string byEmpFullName, string byEmpPosNo, string byEmpPosName, string remarks)
        {
            bool resultIsCompleted = false;
            // Update more the TaskHistory info.
            this.ActionCode = action.ToString();
            this.ActionBy = byEmpId;
            this.ActionByPosName = string.Format("{0} - {1}", byEmpPosNo, byEmpPosName);
            this.ActionByFullName = byEmpFullName;
            this.Remark = remarks;
            this.PassStep = true;

            // Update more the TaskHistory info. split by action by, when to the TaskHistory
            if (action == CommandAction.Approve)
            {
                this.ApprovedBy = byEmpId;
                this.ApprovedWhen = DateTime.Now;
                this.ApprovedByFullName = byEmpFullName;
            }
            else if (action == CommandAction.Forward)
            {
                this.ForwardBy = byEmpId;
                this.ForwardWhen = DateTime.Now;
                this.ForwardByFullName = byEmpFullName;
            }
            else if (action == CommandAction.SendBack)
            {
                this.SendBackBy = byEmpId;
                this.SendBackWhen = DateTime.Now;
                this.SendBackByFullName = byEmpFullName;
            }
            else if (action == CommandAction.SendBackOk)
            {
                this.SendBackOkBy = byEmpId;
                this.SendBackOkWhen = DateTime.Now;
                this.SendBackOkByFullName = byEmpFullName;
            }
            else if (action == CommandAction.Reject)
            {
                this.RejectedBy = byEmpId;
                this.RejectedWhen = DateTime.Now;
                this.RejectedByFullName = byEmpFullName;

                resultIsCompleted = true;
            }
            else if (action == CommandAction.Cancel)
            {
                this.CanceledBy = byEmpId;
                this.CanceledWhen = DateTime.Now;
                this.CanceledByFullName = byEmpFullName;

                resultIsCompleted = true;
            }
            return resultIsCompleted;
        }
    }
}